/* (c)2010-2011 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.web;

import java.beans.PropertyEditorSupport;

import org.ajm.spring.survey.modules.admin.model.AnswerMode;
import org.apache.log4j.Logger;

/**
 * Property editor for {@link org.ajm.spring.survey.modules.admin.model.AnswersType AnswerType} 
 * type properties.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
public class AnswerModePropertyEditor extends PropertyEditorSupport {
    
    static Logger logger = Logger.getLogger(AnswerModePropertyEditor.class);
    
    // ------------------------------------------------------------------------|

    /**
     * Default constructor.
     */
    public AnswerModePropertyEditor() {
        
        super();
    }

    // ------------------------------------------------------------------------|

    /**
     * Sets the property value by parsing a given String.
     */
    @Override
    public void setAsText(String answerModeStr) {
        
        if (logger.isDebugEnabled()) {
            logger.debug("Read string [" + answerModeStr + "] for the answer type field");
        }
        
        AnswerMode answerMode = AnswerMode.SELECTION;
        
        if (answerModeStr.compareTo(AnswerMode.CUSTOM_DESC.toString()) == 0) {
            answerMode = AnswerMode.CUSTOM_DESC;
        }

        setValue(answerMode);
    }

    // ------------------------------------------------------------------------|

    /**
     * Gets the property value as text.
     */
    @Override
    public String getAsText() {
        
        if(getValue() == null) {
            return null;
        }
        
        AnswerMode answerMode = (AnswerMode)getValue();
        return answerMode.getTranslatedName();
    }

    // ------------------------------------------------------------------------|

}
